package com.fms.kccc;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.fms.kccc.util.Common;
import com.fms.kccc.util.NotificationUtils;
import com.google.android.gcm.GCMBaseIntentService;

public class GCMIntentService extends GCMBaseIntentService {

	private final int MAX_NUM = 1;

	public GCMIntentService() {
		super(Common.Variable.SENDER_ID);
		Log.d("kccc", "create GCMIntentService()");
	}

	@Override
	protected void onError(Context arg0, String arg1) {

	}

	@Override
	protected void onMessage(Context context, Intent itent) {
		Log.i("kccc", "Received message");

		if (MainActivity.appRunningSate == 2) {
			MainActivity.numNewPost = MainActivity.numNewPost + 1;
			notifyNewPost(context);
		} else if (MainActivity.appRunningSate == 0) {
			SharedPreferences preferences = PreferenceManager
					.getDefaultSharedPreferences(this);
			boolean isNotifySetting = preferences.getBoolean(
					Common.Variable.NOTIFY_SETTING, true);
			if (isNotifySetting) {
				MainActivity.numNewPost = MainActivity.numNewPost + 1;
				int numNewPost = MainActivity.numNewPost;
				Log.d("kccc", "numNewPost " + numNewPost);
				if (numNewPost >= MAX_NUM) {
					generateNotification(context, numNewPost);
					numNewPost = 0;
				}

			}

		} else if (MainActivity.appRunningSate == 1) {
			SharedPreferences preferences = PreferenceManager
					.getDefaultSharedPreferences(this);
			boolean isNotifySetting = preferences.getBoolean(
					Common.Variable.NOTIFY_SETTING, true);
			notifyNewPost(context);
			if (isNotifySetting) {
				MainActivity.numNewPost = MainActivity.numNewPost + 1;
				int numNewPost = MainActivity.numNewPost;
				Log.d("kccc", "numNewPost " + numNewPost);
				if (numNewPost >= MAX_NUM) {
					generateNotification(context, numNewPost);
					numNewPost = 0;
				}

			}
		}

	}

	private void notifyNewPost(Context context) {
		Intent intent = new Intent(Common.Intent.NOTIFY_NEW_POST);
		intent.putExtra(Common.Variable.MESSAGE, "data");
		LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
	}

	private void generateNotification(Context context, int num) {
		String message = num + " "
				+ context.getResources().getString(R.string.new_post_key);
		int icon = R.drawable.kccc_ic_kccc;
		long when = System.currentTimeMillis();

		NotificationManager notificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		Notification notification = new Notification(icon, message, when);

		String title = context.getString(R.string.app_name);

		Intent notificationIntent = new Intent(context, MainActivity.class);
		// set intent so it does not start a new activity
		notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
				| Intent.FLAG_ACTIVITY_SINGLE_TOP);
		PendingIntent intent = PendingIntent.getActivity(context, 0,
				notificationIntent, 0);
		notification.setLatestEventInfo(context, title, message, intent);
		notification.flags |= Notification.FLAG_AUTO_CANCEL;

		// Play default notification sound
		notification.defaults |= Notification.DEFAULT_SOUND;

		// Vibrate if vibrate is enabled
		notification.defaults |= Notification.DEFAULT_VIBRATE;
		notificationManager.notify(0, notification);
	}

	@Override
	protected void onRegistered(Context context, String regID) {
		Log.i("kccc", "Device registered: regId = " + regID);
		NotificationUtils.register(context, regID);
	}

	@Override
	protected void onUnregistered(Context context, String regID) {
		Log.i("kccc", "Device unregistered: regId = " + regID);
		NotificationUtils.unregister(context, regID);
	}

}
